﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Home
{
	public class Home
	{
		public float StartAmount = 20.27f;
		public List<Route> Routes = new List<Route>();

		public Home()
		{
		}

		public void Load(string fn)
		{
			TextReader R = File.OpenText(fn);
			int N = int.Parse(R.ReadLine());
			for (int i = 0; i < N; i++)
			{
				Route r = new Route(DateTime.MinValue, DateTime.MinValue, "");
				r.Load(R);
				Routes.Add(r);
			}
			R.Close();
		}
		public void Save(string fn)
		{
			TextWriter W = File.CreateText(fn);
			W.WriteLine(Routes.Count);
			foreach (var r in Routes)
				r.Save(W);
			W.Close();
		}
	}
	public class Route
	{
		const float DAILY = 25.00f;
		const float FUEL_CONS = 6.5f;
		public DateTime Start, End;
		public string Name;
		public float Budget, Spare;
		public int SiteDays, WorkDays;
		public int SiteDist;

		public float SiteSalary
		{
			get
			{
				return DAILY * SiteDays;
			}
		}
		public int Distance_Daily
		{
			get
			{
				return SiteDist * SiteDays;
			}
		}

		public List<RouteEntry> Entries = new List<RouteEntry>();

		public List<KeyValuePair<String, int>> Lengths = new List<KeyValuePair<string, int>>();
		public void AddLength(string name, int dist)
		{
			Lengths.Add(new KeyValuePair<string, int>(name, dist));
		}
		public int TotalLength
		{
			get
			{
				return Lengths.Sum(x => x.Value) + SiteDist;
			}
		}
		public float TotalTanked
		{
			get
			{
				return Entries.Sum(x => x.name == "Tankolás" ? x.amount : 0);
			}
		}
		public float TankCover
		{
			get
			{
				return TotalLength * 6.5f / 100 * FuelPrice;
			}
		}
		public float TankLeft
		{
			get
			{
				return TankCover - TotalTanked;
			}
		}
		public float FuelPrice
		{
			get
			{
				return Entries.Count == 0 ? 0 : Entries.Max(x => x.fuelprice);
			}
		}
		public float Daily
		{
			get
			{
				return ((End - Start).Days + 1) * 25;
			}
		}
		public float Car
		{
			get
			{
				return TotalLength * 0.03f;
			}
		}
		public float Pushed
		{
			get
			{
				return Daily - Own;
			}
		}
		public float Spent
		{
			get
			{
				return Entries.Sum(x => x.iscompany ? 0 : x.amount);
			}
		}
		public float SpentComp
		{
			get
			{
				return Entries.Sum(x => x.iscompany ? x.amount : 0);
			}
		}

		public float OwnRemain
		{
			get
			{
				return Own - Spent;
			}
		}
		public float CompRemain
		{
			get
			{
				return Company - SpentComp;
			}
		}
		public void AddEntry(int y, int m, int d, int h, int min, string name, float amount, bool iscomp, float fp = -1)
		{
			Entries.Add(new RouteEntry(new DateTime(y, m, d, h, min, 0), name, amount, iscomp, fp));
		}
		public void AddFuel(int y, int m, int d, int h, int min, float amount, float fp)
		{
			AddEntry(y, m, d, h, min, "Tankolás", amount, false, fp);
		}
		public void SetBudget(float iown, float icomp, float ispare)
		{
			Own = iown;
			Company = icomp;
			Spare = ispare;
		}
		public override string ToString()
		{
			return Start.ToString("yyyy-MM-dd") + " - " + End.ToString("yyyy-MM-dd") + " " + Name;
		}
		public Route(DateTime start, DateTime end,string iname)
		{
			Start = start;
			End = end;
			Name = iname;
		}
		public void Save(TextWriter W)
		{
			W.WriteLine(Start);
			W.WriteLine(End);
			W.WriteLine(Name);
			W.WriteLine(Own + " " + Company + " " + Spare);
			W.WriteLine(Entries.Count);
			foreach (var e in Entries)
				e.Save(W);
			W.WriteLine(Lengths.Count);
			foreach (var l in Lengths)
			{
				W.WriteLine(l.Key + ";" + l.Value);
			}
		}
		public void Load(TextReader R)
		{
			Start = DateTime.Parse(R.ReadLine());
			End = DateTime.Parse(R.ReadLine());
			Name = R.ReadLine();
			string[] amounts = R.ReadLine().Split(' ');
			Own = float.Parse(amounts[0]);
			Company = float.Parse(amounts[0]);
			Spare = float.Parse(amounts[0]);
		}
	}
	
	[Serializable]
	public struct RouteEntry
	{
		public DateTime time;
		public string name;
		public float amount;
		public bool iscompany;
		public float fuelprice;
		public RouteEntry(DateTime itime, string iname, float iamount, bool icomp, float ifuel = -1)
		{
			time = itime;
			name = iname;
			amount = iamount;
			iscompany = icomp;
			fuelprice = ifuel;
		}
		public void Save(TextWriter W)
		{
			W.WriteLine(time + ";" + name + ";" + amount.ToString("0.00") + ";" + iscompany + ";" + fuelprice.ToString("0.000"));
		}
		public void Load(TextReader R)
		{
		}
	}
}